﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HitchHikersGuideToTheGalaxy.Models;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.IO;

namespace HitchHikersGuideToTheGalaxy.Helpers
{
    public class XmlParser
    {
        private static Umferdastofa2.USXMLWS us = new HitchHikersGuideToTheGalaxy.Umferdastofa2.USXMLWS();
        private static string userName = "sveinnbjarki";
        private static string password = "435t.4tg-wr";
        private static string xmlVersion = "7";

        public static Vehicle PopulateVehicle(string ssn, string numberPlate, IFuelRepository fuelRepo) {
            fuelRepo = new FuelRepository();
            Vehicle vehicle = new Vehicle();
            String xmlString = UmferdaStofaResponse(ssn, numberPlate);

            if (xmlString.Equals("")) 
            {
                return vehicle;
            }

            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                reader.ReadToFollowing("make");
                vehicle.Type = reader.ReadElementContentAsString();

                reader.ReadToFollowing("vehcom");
                vehicle.SubType = reader.ReadElementContentAsString();
                                   
                reader.ReadToFollowing("color");
                vehicle.Color = reader.ReadElementContentAsString();

                reader.ReadToFollowing("engine");
                vehicle.FuelId = fuelRepo.GetFuelId(reader.ReadElementContentAsString());

                reader.ReadToFollowing("pass");
                vehicle.NumberOfPassengers = Convert.ToInt32(reader.ReadElementContentAsString());

                reader.ReadToFollowing("urban");
                try
                {
                    string sUrban = reader.ReadElementContentAsString();
                    String[] sUrban2 = sUrban.Split('.');
                    String sUrban3 = sUrban2[0] + "," + sUrban2[1];
                    decimal dUrban = Convert.ToDecimal(sUrban3);
                    vehicle.FuelUsageUrban = Convert.ToDecimal(dUrban);
                }
                catch
                {
                    vehicle.FuelUsageUrban = Convert.ToDecimal("0");
                }
                

                reader.ReadToFollowing("extraurban");
                try
                {
                    string sExtraUrban = reader.ReadElementContentAsString();
                    String[] sExtraUrban2 = sExtraUrban.Split('.');
                    String sExtraUrban3 = sExtraUrban2[0] + "," + sExtraUrban2[1];
                    decimal dExtraUrban = Convert.ToDecimal(sExtraUrban3);
                    vehicle.FuelUsageRural = Convert.ToDecimal(dExtraUrban);
                }
                catch
                {
                    vehicle.FuelUsageRural = Convert.ToDecimal("0");
                }
                
                reader.ReadToFollowing("combined");
                try
                {
                    string sCombined = reader.ReadElementContentAsString();
                    String[] sCombined2 = sCombined.Split('.');
                    String sCombined3 = sCombined2[0] + "," + sCombined2[1];
                    decimal dCombined = Convert.ToDecimal(sCombined3);
                    vehicle.FuelUsageCombined = Convert.ToDecimal(dCombined);
                }
                catch
                {
                    vehicle.FuelUsageCombined = Convert.ToDecimal("0");
                }
                

                vehicle.NumberPlate = numberPlate;

                //Response.Write(make + " " + vehcom + " " + color + " " + engine + " " + pass + " " + urban + " " + extraurban + " " + combined);
            }


            return vehicle;
        }

        private static String UmferdaStofaResponse(String ssn, String numberPlate) {
            try {
                return us.basicVehicleInformation(userName, password, xmlVersion, ssn, numberPlate, "", "");
            } catch {
                return "";
            }
        }


    }

}
